package com.why.cachecloud.web.controller;

import cn.hutool.crypto.SecureUtil;
import com.why.cachecloud.configuration.constant.AppAuditType;
import com.why.cachecloud.entity.AppAudit;
import com.why.cachecloud.entity.AppUser;
import com.why.cachecloud.web.enums.SuccessEnum;
import com.why.cachecloud.web.util.AppDingTalkUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 注册用户管理(页面没有权限限制)
 * 
 * @Author why
 * @Date 2014年10月28日
 * @Time 上午10:49:32
 */
@Controller
@RequestMapping("/user")
public class UserController extends BaseController{
    
    @Resource
    private AppDingTalkUtil appDingTalkUtil;
    
    /**
     * 注册用户页面
     */
    @RequestMapping(value = "/register")
    public ModelAndView userRegister(Model model, Integer success) {
        model.addAttribute("success", success);
        return new ModelAndView("user/userRegister");
    }
    
    /**
     * 注册用户申请
     */
    @RequestMapping(value = "/apply")
    public ModelAndView doAddUser(String name, String chName, String email, String mobile,String password, Integer type, Long userId, Integer isAlert,String typeName) {
        SuccessEnum success = SuccessEnum.SUCCESS;
        try {
            //保存用户(type=-1为无效用户,需要审批)
            AppUser appUser = AppUser.buildFrom(userId, name, chName, email, mobile, SecureUtil.md5(SecureUtil.md5(password)), type, isAlert);
            userService.save(appUser);
            //提交审批
            AppAudit appAudit = appService.saveRegisterUserApply(appUser, AppAuditType.REGISTER_USER_APPLY, typeName);
            appDingTalkUtil.noticeUserResult(appUser, appAudit, typeName);
        } catch (Exception e) {
            success = SuccessEnum.FAIL;
            logger.error(e.getMessage(), e);
        }
        return new ModelAndView("redirect:/user/register?success=" + success.value());
    }
    
    
    @RequestMapping(value = "/checkUserNameExist")
    public ModelAndView doCheckUserNameExist(HttpServletResponse response, String userName) {
        AppUser appUser = userService.getByName(userName);
        if (appUser != null) {
            write(response, String.valueOf(SuccessEnum.SUCCESS.value()));
        } else {
            write(response, String.valueOf(SuccessEnum.FAIL.value()));
        }
        return null;
    }

    /**
     * 修改密码
     *
     * @param userId
     * @return
     */
    @RequestMapping(value = "/updatePwd")
    @ResponseBody
    public Map<String, Object> updatePwd(Long userId, String password) {
        String pwd = SecureUtil.md5(SecureUtil.md5(password));
        Boolean result = userService.modifyPassword(userId, pwd);
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("status", result);
        return resultMap;
    }
}
